package com.xym.manage.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.abel533.entity.Example;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xym.manage.pojo.BasePojo;



public abstract class BaseService<T extends BasePojo> {

	@Autowired
	public abstract  Mapper<T> getMapper();
	
	// 当前Service上泛型的字节码对象   创建Example（clazz）删除时传入
	/*private Class<T> clazz;
	
	{// 读取当前类上的泛型
		ParameterizedType  type = (ParameterizedType) this.getClass().getGenericSuperclass();
		this.clazz=(Class<T>) type.getActualTypeArguments()[0];
	}*/
	
	 /**
     * 根据主键进行查询
     * @param id
     * @return
     */
    public T queryById(Long id){
        return this.getMapper().selectByPrimaryKey(id);
    }
    
    public Integer queryCount(T recrod){
    	return this.getMapper().selectCount(recrod);
    }
    
    /**
     * 查询全部
     * @return
     */
    public List<T> queryAll(){
        return this.getMapper().select(null);
    }
    
    /**
     * 根据条件查询1条数据。
     * @param record
     * @return
     */
    public T queryOne(T record){
        return this.getMapper().selectOne(record);
    }
    
    /**
     * 根据条件查询多条数据
     * @param record
     * @return
     */
    public List<T> queryListByWhere(T record){
        return this.getMapper().select(record);
    }
    
    /**
     * 根据条件分页查询
     * @param record
     * @return
     */
    public PageInfo<T> queryPageListByWhere(T record, Integer page, Integer rows){
        PageHelper.startPage(page, rows);
        List<T> list = this.getMapper().select(record);
        return new PageInfo<>(list);
    }
    
    /**
     * 插入数据
     * @param record
     * @return
     */
    public Integer save(T record){
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return this.getMapper().insert(record);
    }
    
    /**
     * 插入数据,只操作record中的非空属性
     * @param record
     * @return
     */
    public Integer saveSelective(T record){
        record.setCreated(new Date());
        record.setUpdated(record.getCreated());
        return this.getMapper().insertSelective(record);
    }
    
    
    /**
     * 更新数据
     * @param record
     * @return
     */
    public Integer update(T record){
        record.setUpdated(new Date());
        return this.getMapper().updateByPrimaryKey(record);
    }
    
    /**
     * 更新数据,只操作record中的非空属性
     * @param record
     * @return
     */
    public Integer updateSelective(T record){
        record.setUpdated(new Date());
        // 保证created字段不会被修改
        record.setCreated(null);
        return this.getMapper().updateByPrimaryKeySelective(record);
    }
    
    /**
     * 根据主键进行删除
     * @param id
     * @return
     */
    public Integer deleteById(Long id){
        return this.getMapper().deleteByPrimaryKey(id);
    }
    
    /**
     * 批量删除
     * @param id
     * @return
     */
    public Integer deleteByIds(String property, List<Object> ids,Class<?> clazz){
        Example example = new Example(clazz);
        example.createCriteria().andIn(property, ids);
        return this.getMapper().deleteByExample(example);
    }
    
    /**
     * 根据条件删除
     * @param id
     * @return
     */
    public Integer deleteByWhere(T record){
        return this.getMapper().delete(record);
    }
}
